// TODO 快速登录
var Common = function() {
var $body = $('body');
var initialize = function() {
$.ajaxSetup({
global: false,
type: 'POST',
timeout: 60000,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
if('undefined' !== typeof (layer)) {
layer.config({
extend: 'default/retina.css',
shade: 0.2
});
}
if('undefined' !== typeof (NProgress)) {
NProgress.configure({
showSpinner: false
});
}
};
var handleEvent = function() {
if($.support.pjax) {
var $container = $('#pjax-container');
$(document).pjax('a[data-pjax]', '#pjax-container', {
type: 'GET',
timeout: 30000,
scrollTo: $container.length > 0 ? $container.offset().top : 0
});
$(document).on('pjax:send', function() {
NProgress.start();
Common.loading();
});
$(document).on('pjax:complete', function() {
Common.unloading();
NProgress.done(true);
$('#pjax-container').find('img.lazy').lazyload({
effect: 'fadeIn',
});
});
}
$body.find('img.lazy').lazyload({
effect: 'fadeIn',
});
$body.find('[data-toggle="tooltip"]').tooltip({
delay: {
show: 300,
hide: 100
}
});
$body.find('a').on('click', function() {
$body.find('[data-toggle="tooltip"]').tooltip('hide');
});
if('undefined' !== typeof ($.fn.magnificPopup)) {
$body.find('.popup-image').each(function() {
$(this).magnificPopup({
type: 'image',
mainClass: 'mfp-with-zoom',
zoom: {
enabled: true,
duration: 300,
easing: 'ease-in-out',
opener: function(openerElement) {
return openerElement.is('img') ? openerElement : openerElement.find('img');
}
}
});
});
$body.find('.popup-gallery').each(function() {
$(this).magnificPopup({
delegate: 'a',
type: 'image',
tLoading: 'Loading image #%curr%...',
mainClass: 'mfp-img-mobile',
gallery: {
enabled: true,
navigateByImgClick: true,
preload: [0, 1]
},
image: {
tError: 'The image #%curr% could not be loaded.',
}
});
});
}
$body.find('.dropdown').each(function() {
var $dropdown = $(this);
var $menu = $dropdown.find('.dropdown-menu');
if($menu.data('click-unclose')) {
$menu.on('click', function(e) {
e.stopPropagation();
});
}
});
$body.on('click', '.ajax-action', function() {
var action = $(this).data('action');
var method = $(this).data('method');
var confirmMsg = $(this).data('confirm');
Common.layerComfirm(confirmMsg, function(idx) {
layer.close(idx);
Common.layerLoad(function() {
$.ajax({
url: action,
type: 'POST',
dataType: 'json',
data: {
'_method': method
},
success: function(response) {
if(1 == response['status']) {
Common.layerMsg(response['msg'], function() {
var data = response['data'];
if(data['url'] && '' != data['url']) {
window.location.replace(data['url']);
} else {
window.location.reload(true);
}
}, 2000);
} else {
Common.layerAlert(2, response['msg']);
}
},
error: function() {
Common.layerMsg(LANG.LANG_0440);
}
});
});
});
return false;
});
$body.find('.form-change-submit').on('change', function() {
var form = $(this).data('form');
if(form && $(form).length > 0) {
$(form).submit();
}
return false;
});
$body.find('.form-control-upload').each(function() {
var $upload = $(this);
$upload.find('input:file').bind('change', function() {
$upload.find('input:text').val($(this).val());
return false;
});
$upload.find('.btn-upload').bind('click', function() {
$upload.find('input:file').trigger('click');
return false;
});
});
// ScrollToTop
var scrollToTop = {
setting: {
startline: 100,
scrollto: 0,
scrollduration: 400,
fadeduration: [500, 100]
},
controlHTML: '',
controlattrs: {offsetx: 30, offsety: 80},
anchorkeyword: "#top",
state: {
isvisible: false,
shouldvisible: false
}, scrollup: function() {
if(!this.cssfixedsupport) {
this.$control.css({opacity: 0});
}
var dest = isNaN(this.setting.scrollto) ? this.setting.scrollto : parseInt(this.setting.scrollto);
if(typeof dest == "string" && jQuery("#" + dest).length == 1) {
dest = jQuery("#" + dest).offset().top;
} else {
dest = 0;
}
this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
}, keepfixed: function() {
var $window = jQuery(window);
var controlx = $window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx;
var controly = $window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety;
this.$control.css({left: controlx + "px", top: controly + "px"});
}, togglecontrol: function() {
var scrolltop = jQuery(window).scrollTop();
if(!this.cssfixedsupport) {
this.keepfixed();
}
this.state.shouldvisible = (scrolltop >= this.setting.startline) ? true : false;
if(this.state.shouldvisible && !this.state.isvisible) {
this.$control.stop().animate({opacity: 1}, this.setting.fadeduration[0]);
this.state.isvisible = true;
} else {
if(this.state.shouldvisible == false && this.state.isvisible) {
this.$control.stop().animate({opacity: 0}, this.setting.fadeduration[1]);
this.state.isvisible = false;
}
}
}, init: function() {
jQuery(document).ready(function($) {
var mainobj = scrollToTop;
var iebrws = document.all;
mainobj.cssfixedsupport = !iebrws || iebrws && document.compatMode == "CSS1Compat" && window.XMLHttpRequest;
mainobj.$body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $("html") : $("body")) : $("html,body");
mainobj.$control = $('
' + mainobj.controlHTML + "
").css({
position: mainobj.cssfixedsupport ? "fixed" : "absolute",
bottom: mainobj.controlattrs.offsety,
right: mainobj.controlattrs.offsetx,
opacity: 0,
cursor: "pointer"
}).attr({title: "Top"}).click(function() {
mainobj.scrollup();
return false;
}).appendTo("body");
if(document.all && !window.XMLHttpRequest && mainobj.$control.text() != "") {
mainobj.$control.css({width: mainobj.$control.width()});
}
mainobj.togglecontrol();
$('a[href="' + mainobj.anchorkeyword + '"]').click(function() {
mainobj.scrollup();
return false;
});
$(window).bind("scroll resize", function(e) {
mainobj.togglecontrol();
});
});
}
};
scrollToTop.init();
};
return {
layerAlert: function(icon, msg, callback) {
layer.closeAll('loading');
layer.alert(msg, {
icon: icon,
title: LANG['LANG_0445'],
closeBtn: 0,
btn: ['Close'],
resize: false
}, function(idx) {
if(callback) {
callback(idx);
} else {
layer.close(idx);
}
});
},
layerMsg: function(msg, callback, time) {
layer.closeAll('loading');
layer.msg(msg, {
shade: false,
time: time || 2000
}, function() {
if(callback) callback();
});
},
layerComfirm: function(msg, yes, cancel) {
layer.closeAll('loading');
layer.confirm(msg || LANG['LANG_0550'], {
icon: 3,
title: LANG['LANG_0445'],
// shade: 0.2,
resize: false,
btn: ['OK', 'Cancel']
}, function(idx) {
if(yes) yes(idx);
}, function(idx) {
if(cancel) {
cancel(idx);
} else {
layer.close(idx);
}
});
},
layerLoad: function(callback, type) {
type = type || 1;
layer.load(type, {
shade: 0.05,
success: function() {
if(callback) callback();
}
});
},
btnLoad: function($btn) {
if($($btn).length > 0) {
var loadingText = $btn.data('loading-text') || 'Loading...';
if(!$btn.data('loading-status')) {
$btn.data('loading-status', true);
$btn.attr({
'data-loading-status': true,
'data-original-text': $btn.html(),
'disabled': 'disabled',
'tabindex': '-1'
}).prop('disabled', 'disabled').html(loadingText);
}
}
},
btnUnload: function($btn) {
if($($btn).length > 0) {
var originalText = $btn.data('original-text') || '';
if($btn.data('loading-status')) {
$btn.data('loading-status', false);
$btn.removeAttr('data-loading-status').removeAttr('data-original-text').removeAttr('disabled').removeAttr('tabindex');
$btn.removeProp('disabled').html(originalText);
}
}
},
loading: function(el, callback) {
var $el = $(el);
var isfull = ($el.length === 0);
var $container = isfull ? $body : $el;
if($container.find('.loading-mask').length === 0) {
$container.prepend('');
}
if(callback) {
callback($container);
}
},
unloading: function(el) {
var $el = $(el);
var $container = ($el.length === 0) ? $body : $el;
$container.find('.loading-mask').remove();
},
getCkbCount: function(el, selected) {
if('undefined' === typeof (el)) {
return false;
}
if(selected) {
return $(el).find('tbody input[type="checkbox"].selectable-item:not(.disabled):checked').length;
} else {
return $(el).find('tbody input[type="checkbox"].selectable-item:not(.disabled)').length;
}
},
getCkbVals: function(el, selected) {
if('undefined' === typeof (el)) {
return false;
}
var elCkbs = null;
var ids = [];
if(selected) {
elCkbs = $(el).find('tbody input[type="checkbox"].selectable-item:checked');
} else {
elCkbs = $(el).find('tbody input[type="checkbox"].selectable-item');
}
for(var i = 0, len = elCkbs.length; i < len; i++) {
ids.push($(elCkbs[i]).val());
}
return ids.join(',');
},
resetCkb: function(el, selected) {
el = ('undefined' === typeof (el)) ? $('.table') : el;
$(el).find('tbody input:checkbox.selectable-item').each(function() {
if(selected && !$(this).prop('disabled') && !$(this).prop('readonly')) {
$(this).closest('tr').addClass('table-active');
$(this).attr('checked', 'checked').prop('checked', true).prop('checked', 'checked');
} else {
$(this).closest('tr').removeClass('table-active');
$(this).prop('checked', false).removeAttr('checked').removeProp('checked');
}
});
},
init: function() {
initialize();
handleEvent();
}
};
}();
var Quote = function() {
var TS_QUOTES = 'ts_quotes';
var MAX_NUMS = 50;
var initialize = function() {
if(!window.localStorage) {
Common.layerAlert(4, 'Your browser either does not support localStorage, or it is disabled. Either upgrade your browser, or enable localStorage');
}
};
return {
getItems: function() {
var items = null;
if(window.localStorage) {
if(window.localStorage.hasOwnProperty(TS_QUOTES)) {
items = JSON.parse(window.localStorage.getItem(TS_QUOTES));
}
}
return (null === items || false === items) ? {} : items;
},
addItems: function(data, callback) {
if(window.localStorage) {
var quotes = this.getItems();
if(Object.keys(quotes).length >= MAX_NUMS) {
Common.layerMsg('Your inquiry form is full and you need to clean at least one item before you can add new products.');
return false;
}
var part = data.hasOwnProperty('part') ? data['part'] : '';
var brand = data.hasOwnProperty('brand') ? data['brand'] : '';
var description = data.hasOwnProperty('description') ? data['description'] : '';
var quantity = data.hasOwnProperty('quantity') ? data['quantity'] : '';
var hope_price = data.hasOwnProperty('hope_price') ? data['hope_price'] : '';
if($.trim(part) === '') {
return false;
}
var key = $.md5(part + '' + brand);
if(quotes.hasOwnProperty(key)) {
quotes[key]['quantity'] = parseInt(quotes[key]['quantity']) + parseInt(quantity);
quotes[key]['hope_price'] = hope_price;
} else {
quotes[key] = {
'part': part,
'brand': brand,
'description': description,
'quantity': quantity,
'hope_price': hope_price,
};
}
window.localStorage.setItem(TS_QUOTES, JSON.stringify(quotes));
if(IS_LOGIN) {
this.refreshList(function() {
if(callback) callback(key);
});
return;
}
if(callback) callback(key);
} else {
if(callback) callback(false);
}
},
updateItem: function(key, data, callback) {
if('undefined' === typeof (key)) {
return false;
}
if(window.localStorage) {
var items = this.getItems();
if(items.hasOwnProperty(key)) {
items[key] = {
'part': data.hasOwnProperty('part') ? data['part'] : '',
'brand': data.hasOwnProperty('brand') ? data['brand'] : '',
'description': data.hasOwnProperty('description') ? data['description'] : '',
'quantity': data.hasOwnProperty('quantity') ? data['quantity'] : '',
'hope_price': data.hasOwnProperty('hope_price') ? data['hope_price'] : '',
};
window.localStorage.setItem(TS_QUOTES, JSON.stringify(items));
}
if(IS_LOGIN) {
$.ajax({
url: URL_INQUIRY_UPDATE,
type: 'PUT',
data: {
key: key,
data: data,
},
dataType: 'json',
success: function() {
if(callback) callback(true);
},
error: function() {
if(callback) callback(false);
}
});
return;
}
if(callback) callback(true);
return;
}
if(callback) callback(false);
},
removeItem: function(key, callback) {
if('undefined' === typeof (key)) {
return false;
}
if(window.localStorage) {
var items = this.getItems();
keys = key.split(',');
for(var i = 0; i < keys.length; i++) {
if(Object.keys(items).length > 0 && items.hasOwnProperty(keys[i])) {
delete items[keys[i]];
}
}
window.localStorage.setItem(TS_QUOTES, JSON.stringify(items));
if(IS_LOGIN) {
$.ajax({
url: URL_INQUIRY_DESTROY,
type: 'DELETE',
data: {
key: key
},
dataType: 'json',
success: function() {
if(callback) callback(true);
},
error: function() {
if(callback) callback(false);
}
});
return;
}
if(callback) callback(true);
} else {
if(callback) callback(false);
}
},
removeAll: function(callback) {
if(window.localStorage) {
window.localStorage.removeItem(TS_QUOTES);
if(IS_LOGIN) {
$.ajax({
url: URL_INQUIRY_DESTROY,
type: 'DELETE',
data: {
all: 1,
},
dataType: 'json',
success: function() {
if(callback) callback(true);
},
error: function() {
if(callback) callback(false);
}
});
return;
}
if(callback) callback(true);
} else {
if(callback) callback(false);
}
},
refreshList: function(callback) {
if(IS_LOGIN) {
var items = this.getItems();
if(Object.keys(items).length > 0) {
$.ajax({
url: URL_INQUIRY_REFRESH,
type: 'POST',
data: {
items: items
},
dataType: 'json',
success: function() {
window.localStorage.removeItem(TS_QUOTES);
if(callback) callback(true);
},
error: function() {
if(callback) callback(false);
}
});
return;
}
}
if(callback) callback(true);
},
getQuoteNums: function() {
if(IS_LOGIN) {
this.refreshList(function() {
$.ajax({
url: API_INQUIRY_GETNUMS,
type: 'POST',
dataType: 'json',
success: function(response) {
if(1 === response['status']) {
$('.quick-nums-quote').text(response['data']).show();
}
}
});
});
} else {
$('.quick-nums-quote').text(Object.keys(this.getItems()).length).show();
}
},
init: function() {
initialize();
}
};
}();
var User = function() {
var $body = $('body');
var initialize = function() {
};
var handleEvent = function() {
$body.on('click', '.favorite-status', function() {
var $link = $(this);
var action = $link.data('action');
var method = $link.data('method');
var source_id = $link.data('source');
var obj_id = $link.data('id');
var type = $link.data('type');
if(!IS_LOGIN) {
Common.layerMsg(LANG['LANG_0538']);
return false;
}
Common.loading(null, function() {
$.ajax({
url: action,
type: 'POST',
dataType: 'json',
data: {
'type': type,
'source_id': source_id,
'product_id': obj_id,
'_method': method
},
success: function(response) {
Common.unloading();
Common.layerMsg(response['msg']);
if(1 == response['status']) {
$link.hide().siblings('.favorite-status').show();
} else {
}
},
error: function() {
Common.unloading();
Common.layerMsg(LANG['LANG_0440']);
}
});
});
return false;
});
};
return {
init: function() {
initialize();
handleEvent();
}
};
}();
$(function() {
Common.init();
Quote.init();
User.init();
$(document).ready(function() {
Quote.getQuoteNums();
var $body = $('body');
$body.find('.form-typeahead').each(function() {
var $form = $(this);
var $texxLoading = $form.find('.field-loading');
var $textKeyword = $form.find('.text-keyword');
var $btnSearch = $form.find('.btn-search');
var typeaheadParts = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('part'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: API_PRODUCT_SEARCH_PARTS + '?keyword=%QUERY',
wildcard: '%QUERY'
}
});
typeaheadParts.initialize();
$textKeyword.typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
limit: 30,
name: 'parts',
displayKey: 'part',
source: typeaheadParts.ttAdapter()
});
$textKeyword.bind('typeahead:asyncrequest', function() {
$texxLoading.addClass('active');
$btnSearch.hide();
});
$textKeyword.bind('typeahead:asyncreceive', function() {
$texxLoading.removeClass('active');
$btnSearch.show();
});
$textKeyword.bind('typeahead:asynccancel', function() {
$texxLoading.removeClass('active');
$btnSearch.show();
});
$textKeyword.bind('typeahead:select', function() {
setTimeout(function() {
$form.submit();
}, 100);
});
});
var $control = $body.find('#control');
$(window).scroll(function() {
if($(this).scrollTop() > 400) {
$control.addClass('fixed fadeInDown');
} else {
$control.removeClass('fixed fadeInDown');
}
});
$(window).scroll();
});
});